package com.tianlu.store.common;

import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author liangdl
 * @since 2023/4/1 18:13
 */
@ControllerAdvice(basePackages = {"com.tianlu.store.controller"})
public class ControllerAspect {
    @ResponseBody
    @ExceptionHandler(ServiceException.class)
    public ResponseResult handleExceptions(ServiceException e) {
        return ResponseResult.fail(e.getResultCode(), e.getAppendErrorMessage(), e.getAppendUserMessage());
    }

    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseResult handleExceptions(MethodArgumentNotValidException e) {
        return ResponseResult.fail(ResultCode.CODE_402, e.getMessage().substring(e.getMessage().lastIndexOf("[") + 1, e.getMessage().lastIndexOf("]") - 1));
    }

}
